package com.apache.poi.word.controller;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;

public class Page {

    public static void main(String[] args) throws Exception {

//        XWPFDocument document = new XWPFDocument();
//        XWPFDocument document = new XWPFDocument(new FileInputStream("/Users/huangguoye/IdeaProjects/gitee/word/word-demo/src/main/resources/docs/公益课程流程.docx"));
        XWPFDocument document = new XWPFDocument(new FileInputStream("/Users/huangguoye/IdeaProjects/gitee/word/word-demo/src/main/resources/docs/秩序册.docx"));
//        XWPFDocument document = new XWPFDocument(new FileInputStream("/Users/huangguoye/IdeaProjects/gitee/word/word-demo/src/main/resources/docs/若依环境使用手册.docx"));

//
//        for (int i = 0; i <100; i++) {
//            //创建新的段落
//            XWPFParagraph p1 = document.createParagraph();
//            XWPFRun run1 = p1.createRun();
//            run1.addBreak();
//            run1.setText("举办广西壮族自治区第十四届运动会是以习近平新时代中国特色社会主义思想为指导，深入贯彻落实党的十九大精神，进一步深化我区体育体制改革，以群众参与，人民满意为导向，增加群众的参与度、获得感和满足感，努力使举办自治区运动会成为推动健康广西建设的重要展示窗口和舞台，成为群众体育与竞技体育协调发展的有效平台，成为弘扬体育文化、传承体育精神、推动广西体育事业改革发展的助推器和风向标，成为全区人民关注度高，热烈、精彩、和谐的高水平体育盛会，为建设体育强区、重振广西体育雄风做出积极贡献。");
//        }



        CTSectPr sectPr = document.getDocument().getBody().getSectPr();
        if (sectPr == null){
            sectPr = document.getDocument().getBody().addNewSectPr();
        }
        CTPageMar pageMar = sectPr.getPgMar();
        if (pageMar == null) {
            pageMar = sectPr.addNewPgMar();
        }
//        //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
//        pageMar.setLeft(BigInteger.valueOf(720));
//        pageMar.setRight(BigInteger.valueOf(720));
//        //1440 Twips = 1440/20 = 72 pt = 72/72 = 1"
//        pageMar.setTop(BigInteger.valueOf(440));
//        pageMar.setBottom(BigInteger.valueOf(440));
//        //45.4 pt * 20 = 908 = 45.4 pt header from top
//        pageMar.setHeader(BigInteger.valueOf(908));
//        //28.4 pt * 20 = 568 = 28.4 pt footer from bottom
//        pageMar.setFooter(BigInteger.valueOf(568));



        // create header-footer
        XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) {
            headerFooterPolicy = document.createHeaderFooterPolicy();
        }

        // create header start
        XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

        XWPFParagraph paragraph = header.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);

        XWPFRun run = paragraph.createRun();
        run.setText("Header");

        // create footer start
        XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

        paragraph = footer.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);

        run = paragraph.createRun();
//        run.setText("Footer");
        CTFldChar fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

        CTText ctText = run.getCTR().addNewInstrText();
        ctText.setStringValue("PAGE  \\* MERGEFORMAT");
        ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
//        setXWPFRunStyle(run,"Arial",10);
        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
        run = paragraph.createRun();
        run.setText("页 总共");
        run = paragraph.createRun();
        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

        run = paragraph.createRun();
        ctText = run.getCTR().addNewInstrText();
        ctText.setStringValue("NUMPAGES  \\* MERGEFORMAT ");
        ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
        setXWPFRunStyle(run,"Arial",10);

        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("end"));

        run = paragraph.createRun();
        run.setText("页");



        document.write(new FileOutputStream("/Users/huangguoye/IdeaProjects/gitee/word/word-demo/src/main/java/com/apache/poi/word/controller/CreateWordHeaderFooterTopBottom002.docx"));

        document.close();

    }

    private  static  void setXWPFRunStyle(XWPFRun r1,String font,int fontSize) {
        r1.setFontSize(fontSize);
        CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr();
//        CTFonts fonts =  rpr.addNewRFonts();
//        fonts.setAscii(font);
//        fonts.setEastAsia(font);
//        fonts.setHAnsi(font);
    }

}
